home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-06-19 | 5.3 KB | 210 lines |
- package symantec.itools.multimedia;
-
-
- import java.awt.Canvas;
- import java.awt.Dimension;
-
- // 01/29/97 TWB Integrated changes from Macintosh
-
- /**
- * NervousText control.
- * Creates animated text in which each letter moves independently of all other letters.
- *
- * @version 1.0, Nov 26, 1996
- * @author Symantec
- */
-
- public class NervousText
- extends Canvas
- implements Runnable
- {
- char separated[];
- String text;
- boolean paused;
- Thread jitter = null;
-
-
- /**
- * Create defalut NervousText object. Default object contains the word "text".
- */
-
- public NervousText()
- {
- paused = false;
- setText("text");
- }
-
- /**
- * Tells this component that it has been added to a container.
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is added to a container. Typically, it is used to
- * create this component's peer.
- *
- * It has been overridden here to start the nervous text thread.
- *
- * @see #removeNotify
- */
-
- public void addNotify()
- {
- super.addNotify();
- jitter = new Thread(this);
- jitter.start();
- }
-
- /**
- * Tells this component that it is being removed from a container.
- * This is a standard Java AWT method which gets called by the AWT when
- * this component is removed from a container. Typically, it is used to
- * destroy the peers of this component and all its subcomponents.
- *
- * It has been overridden here to stop the nervous text thread.
- *
- * @see #addNotify
- */
- public synchronized void removeNotify() {
- if (jitter != null){
- jitter.stop();
- jitter = null;
- }
- super.removeNotify();
- }
-
- /**
- * Makes this component visible.
- * This is a standard Java AWT method which gets called to show this
- * component. If this component was invisible due to a previous hide()
- * call it make this component visible again.
- *
- * @see #hide
- */
- public synchronized void show() {
- super.show();
- if (isVisible()) {
- if (jitter != null)
- jitter.resume();
- }
- }
-
- /**
- * Makes this component invisible.
- * This is a standard Java AWT method which gets called to hide
- * this component. A hidden component cannot be seen by the user nor
- * does it take up space in its container, but it does continue to
- * exist.
- *
- * @see #show
- */
- public synchronized void hide() {
- super.hide();
- if (!isVisible()) {
- if (jitter != null)
- jitter.suspend();
- }
- }
-
- /**
- * Specify the text that will be displayed by NervousText.
- *
- * @param str text to be shown as nervous text
- */
-
- public void setText(String str)
- {
- text = str;
- separated = new char[text.length()];
- text.getChars(0, text.length(), separated, 0);
- }
-
- /**
- * Obtain the text that is currently being displayed.
- *
- * @return text that is being shown by this component
- */
- public String getText()
- {
- return text;
- }
-
- /**
- * NervousText thread body. This method is called by the Java virtual
- * machine to when this thread starts.
- */
- public void run()
- {
- while (true)
- {
-
- try
- {
- Thread.sleep(150);
- }
- catch(Exception e)
- {
- }
- if (!paused)
- {
- repaint();
- }
- }
- }
-
- /**
- * Temporarily suspend the animation of the nervous text.
- * @param f pause the animation of true
- *
- */
- public void pause(boolean f)
- {
- paused = f;
- }
-
- /**
- * Obtain animator's current state.
- *
- * @return true if the animator is paused, false if it is running
- */
-
- public boolean isPaused()
- {
- return paused;
- }
- /**
- * Temporarily suspend the animation of the nervous text. Identical
- * to pause(boolean).
- * @param f pause the animation of true
- *
- */
- public void setPaused(boolean f)
- {
- paused = f;
- }
-
- /**
- * Paints this component using the given graphics context.
- * This is a standard Java AWT method which typically gets called
- * by the AWT to handle painting this component. It paints this component
- * using the given graphics context. The graphics context clipping region
- * is set to the bounding rectangle of this component and its <0,0>
- * coordinate is this component's top-left corner.
- *
- * @param g the graphics context used for painting
- * @see java.awt.Component#repaint
- * @see java.awt.Component#update
- */
- public void paint(java.awt.Graphics g)
- {
- Dimension dim = size();
- g.clipRect(0,0,dim.width,dim.height);
-
- for(int i = 0;i < text.length(); i++)
- {
- int x_coord;
- int y_coord;
- x_coord = (int) (Math.random() * 10 + 20 * i);
- y_coord = (int) (Math.random() * 10 + 36);
- g.drawChars(separated, i, 1, x_coord, y_coord);
- }
- }
- }
-